﻿// <ParaBellum for logging mobile worker.>
//     Copyright (C) 2013  APKO
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace ParaBellum {
    /// <summary>
    ///     Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow {
        public MainWindow() {
            this.InitializeComponent();
            var userPrefs = new UserPreferences();
            this.Height = userPrefs.WindowHeight;
            this.Width = userPrefs.WindowWidth;
            this.Top = userPrefs.WindowTop;
            this.Left = userPrefs.WindowLeft;
            this.WindowState = userPrefs.WindowState;
            this.MainPage.AdoptedModue.AddNewOrder.Click += this.AddNewOrder_Click;
            this.OrderPage.Cancel.Click += this.Cancel_Click;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e) {
            this.Wait.IsActive = true;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, ea) => {
                                 DispatchService.Invoke(delegate {
                                                            this.OrderPage.IsEnabled = false;
                                                            this.MainPage.IsEnabled = false;
                                                            this.MainTab.IsSelected = true;
                                                        });
                                 Thread.Sleep(10000);
                             };
            worker.RunWorkerCompleted += (o, ea) => {
                                             this.MainPage.IsEnabled = true;
                                             this.Wait.IsActive = false;
                                         };
            worker.RunWorkerAsync();
        }

        private void AddNewOrder_Click(object sender, RoutedEventArgs e) {
            this.Wait.IsActive = true;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, ea) => {
                                 DispatchService.Invoke(delegate {
                                                            this.MainPage.IsEnabled = false;
                                                            this.OrderPage.IsEnabled = false;
                                                            this.OrderTab.IsSelected = true;
                                                        });
                                 Thread.Sleep(10000);
                             };
            worker.RunWorkerCompleted += (o, ea) => {
                                             this.OrderPage.IsEnabled = true;
                                             this.Wait.IsActive = false;
                                         };
            worker.RunWorkerAsync();
        }

        private void MainWindow1_Closing(object sender, CancelEventArgs e) {
            var userPrefs = new UserPreferences();
            userPrefs.WindowHeight = this.Height;
            userPrefs.WindowWidth = this.Width;
            userPrefs.WindowTop = this.Top;
            userPrefs.WindowLeft = this.Left;
            userPrefs.WindowState = this.WindowState;
            userPrefs.Save();
        }
    }
}